package com.qyhl.module_practice.volunteer.newlist.search;


import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerBean;

import java.util.List;

public class PracticeVolSearchModel implements PracticeVolSearchContract.PracticeVolSearchModel {
    private PracticeVolSearchPresenter mPresenter;

    PracticeVolSearchModel(PracticeVolSearchPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void doSearch(String keyword, String page) {

        EasyHttp.get(PracticeUrl.GET_PRRACTICE_VOLUNTEER_SEARCH)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("key", keyword)
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeVolunteerBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无相关志愿者！", !page.equals("1"));
                        } else {
                            mPresenter.setError("查询失败，请重新尝试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeVolunteerBean> practiceVolunteerBeans) {
                        if (practiceVolunteerBeans != null && practiceVolunteerBeans.size() > 0) {
                            mPresenter.setSearchList(practiceVolunteerBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError("暂无相关志愿者！", !page.equals("1"));
                        }
                    }
                });

    }
}
